﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit;

namespace HYCableTrayConnection
{
	public class HYCableTrayConnect
	{
		public void NewElbowFittingByRevitCommand(IList<CableTray> cableTrays)
		{
			if (cableTrays.Count < 2)
			{
				throw new System.ArgumentNullException("CableTray", "is null");
			}
			Document document = cableTrays[0].Document;
			if (!Common.IsSameCableTrayType(document, cableTrays))
			{
				throw new MyException("桥架的类型不一致");
			}
			HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTrays[0]);
			HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(cableTrays[1]);
			Transform coordinateSystem = hycableTray.CustomConnectorStart.Connector.CoordinateSystem;
			Transform coordinateSystem2 = hycableTray2.CustomConnectorStart.Connector.CoordinateSystem;
			XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
			if (xyz == null)
			{
				throw new MyException("请检查桥架位置");
			}
			Connector appropriateConnector = hycableTray.GetAppropriateConnector(xyz);
			Connector appropriateConnector2 = hycableTray2.GetAppropriateConnector(xyz);
			if (appropriateConnector == null || appropriateConnector2 == null || appropriateConnector.IsConnected || appropriateConnector2.IsConnected)
			{
				throw new MyException("桥架已经连接");
			}
			if (!Common.IsMeetCreateFitting(hycableTray, appropriateConnector, hycableTray2, appropriateConnector2))
			{
				throw new MyException("桥架间的夹角应大于等于90°，小于180°");
			}
			coordinateSystem = appropriateConnector.CoordinateSystem;
			coordinateSystem2 = appropriateConnector2.CoordinateSystem;
			if (coordinateSystem.BasisY.IsAlmostEqualTo(-XYZ.BasisZ, 0.01) && coordinateSystem2.BasisY.IsAlmostEqualTo(-XYZ.BasisZ, 0.01) && hycableTray.IsSamePlan(hycableTray2.CableTray))
			{
				CableTrayFitingSel.GetCableTrayFittingType(document, CableTrayFittingType.HorizontalElbow, hycableTray);
				document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
				return;
			}
			if (coordinateSystem.BasisX.IsAlmostEqualTo(coordinateSystem2.BasisX, 0.01) || coordinateSystem.BasisX.IsAlmostEqualTo(-coordinateSystem2.BasisX, 0.01))
			{
				CableTrayFitingSel.GetCableTrayFittingType(document, CableTrayFittingType.VerticalInnerElbow, hycableTray);
				CableTrayFitingSel.GetCableTrayFittingType(document, CableTrayFittingType.VerticalOuterElbow, hycableTray);
				document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
				return;
			}
		}

		public bool NewElbowFitting(IList<CableTray> cableTrays)
		{
			bool result = false;
			if (cableTrays.Count < 2)
			{
				throw new System.ArgumentNullException("CableTray", "is null");
			}
			Document document = cableTrays[0].Document;
			if (!Common.IsSameCableTrayType(document, cableTrays))
			{
				throw new MyException("桥架的类型不一致");
			}
			HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(cableTrays[0]);
			HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(cableTrays[1]);
			XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
			Transform coordinateSystem = hycableTray.CustomConnectorStart.Connector.CoordinateSystem;
			Transform coordinateSystem2 = hycableTray2.CustomConnectorStart.Connector.CoordinateSystem;
			Transaction transaction = null;
			try
			{
				transaction = new Transaction(document, "Elbow");
				transaction.Start();
				if (coordinateSystem.BasisY.IsAlmostEqualTo(-XYZ.BasisZ, 0.01) && (coordinateSystem2.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || coordinateSystem2.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01)) && (coordinateSystem.BasisX.IsAlmostEqualTo(coordinateSystem2.BasisY, 0.01) || coordinateSystem.BasisX.IsAlmostEqualTo(-coordinateSystem2.BasisY, 0.01)))
				{
					result = new HYElbowConnection(document, hycableTray, hycableTray2).NewVerticalElbowFitting();
				}
				else if (coordinateSystem2.BasisY.IsAlmostEqualTo(-XYZ.BasisZ, 0.01) && (coordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || coordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01)) && (coordinateSystem2.BasisX.IsAlmostEqualTo(coordinateSystem.BasisY, 0.01) || coordinateSystem2.BasisX.IsAlmostEqualTo(-coordinateSystem.BasisY, 0.01)))
				{
					result = new HYElbowConnection(document, hycableTray2, hycableTray).NewVerticalElbowFitting();
				}
				else if (xyz != null)
				{
					Connector appropriateConnector = hycableTray.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hycableTray2.GetAppropriateConnector(xyz);
					if (appropriateConnector == null || appropriateConnector2 == null || appropriateConnector.IsConnected || appropriateConnector2.IsConnected)
					{
						throw new MyException("桥架已经连接");
					}
					if (!Common.IsMeetCreateFitting(hycableTray, appropriateConnector, hycableTray2, appropriateConnector2))
					{
						throw new MyException("桥架间的夹角应大于等于90°，小于180°");
					}
					coordinateSystem = appropriateConnector.CoordinateSystem;
					coordinateSystem2 = appropriateConnector2.CoordinateSystem;
					if (coordinateSystem.BasisY.IsAlmostEqualTo(-XYZ.BasisZ, 0.01) && coordinateSystem2.BasisY.IsAlmostEqualTo(-XYZ.BasisZ, 0.01) && hycableTray.IsSamePlan(hycableTray2.CableTray))
					{
						result = Common.NewElbowFittingByRevitCommand(document, hycableTray, appropriateConnector, appropriateConnector2);
					}
					else if (coordinateSystem.BasisX.IsAlmostEqualTo(coordinateSystem2.BasisX, 0.01) || coordinateSystem.BasisX.IsAlmostEqualTo(-coordinateSystem2.BasisX, 0.01))
					{
						result = Common.NewElbowFittingByRevitCommand(document, hycableTray, appropriateConnector, appropriateConnector2);
					}
				}
				transaction.Commit();
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
                    if ((int)status != 3 && (int)status != 2)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool NewTeeFitting(IList<CableTray> cableTrays)
		{
			bool result = false;
			if (cableTrays.Count < 3)
			{
				throw new System.ArgumentNullException("CableTray", "is null");
			}
			Document document = cableTrays[0].Document;
			if (!Common.IsSameCableTrayType(document, cableTrays))
			{
				throw new MyException("桥架的类型不一致");
			}
			if (Common.IsNoFitting(document, cableTrays[0]))
			{
				throw new MyException("此功能不适用于无配件桥架");
			}
			new List<CableTray>();
			IList<CableTray> list = Common.OrderCableTrayPosition(cableTrays);
			HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(list[0]);
			HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(list[1]);
			HYCableTray<CableTray> hycableTray3 = new HYCableTray<CableTray>(list[2]);
			HYCableTray<CableTray> hycableTray4 = null;
			XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
			Transaction transaction = null;
			try
			{
				transaction = new Transaction(document, "Tee");
				transaction.Start();
				if (xyz != null && hycableTray.IsSamePlan(hycableTray2.CableTray) && hycableTray.IsSamePlan(hycableTray3.CableTray))
				{
					result = Common.NewTeeFittingByRevitCommand(document, xyz, hycableTray, ref hycableTray2, hycableTray3, ref hycableTray4);
				}
				else if (hycableTray2.IsCableTrayOnLine(hycableTray3.CableTray) && hycableTray.IsZAxis())
				{
					if (xyz == null)
					{
						xyz = hycableTray2.GetLineProject(hycableTray.EndXYZ);
					}
					hycableTray4 = Common.SeparateCableTray(ref hycableTray2, hycableTray3, xyz);
					if (hycableTray4 == null)
					{
						throw new MyException("请检查桥架位置");
					}
					Transform coordinateSystem = hycableTray.CustomConnectorStart.Connector.CoordinateSystem;
					Transform coordinateSystem2 = hycableTray4.CustomConnectorStart.Connector.CoordinateSystem;
					if (coordinateSystem.BasisX.IsAlmostEqualTo(coordinateSystem2.BasisX, 0.01) || coordinateSystem.BasisX.IsAlmostEqualTo(-coordinateSystem2.BasisX, 0.01))
					{
						result = new HYTeeConnection(document, hycableTray, hycableTray2, hycableTray4, VerticalCableTrayFittingTypeGroup.VerticalTee, xyz).NewVerticalTeeFitting();
					}
					else if (coordinateSystem.BasisX.IsAlmostEqualTo(coordinateSystem2.BasisZ, 0.01) || coordinateSystem.BasisX.IsAlmostEqualTo(-coordinateSystem2.BasisZ, 0.01))
					{
						result = new HYTeeConnection(document, hycableTray, hycableTray2, hycableTray4, VerticalCableTrayFittingTypeGroup.VerticalSideTee, xyz).NewVerticalTeeFitting();
					}
				}
				else if (hycableTray.IsZAxis() && hycableTray2.IsSamePlan(hycableTray3.CableTray) && hycableTray2.IsIntersection(hycableTray3.CableTray) && Common.IsVerticalIntersection(hycableTray2, hycableTray3))
				{
					xyz = hycableTray2.IntersectionPoint(hycableTray3.CableTray);
					result = new HYTeeConnection(document, hycableTray, hycableTray2, hycableTray3, VerticalCableTrayFittingTypeGroup.VerticalCornerTee, xyz).NewCornerVerticalTeeFitting();
				}
				else if (hycableTray.IsXYPlan() && hycableTray2.IsZAxis() && hycableTray3.IsZAxis())
				{
					if (xyz == null)
					{
						xyz = hycableTray2.GetLineProject(hycableTray.EndXYZ);
					}
					hycableTray4 = Common.SeparateCableTray(ref hycableTray2, hycableTray3, xyz);
					xyz = hycableTray.GetLineProject(hycableTray2.EndXYZ);
					if (hycableTray4 == null)
					{
						throw new MyException("请检查桥架位置");
					}
					Transform coordinateSystem3 = hycableTray.CustomConnectorStart.Connector.CoordinateSystem;
					Transform coordinateSystem4 = hycableTray4.CustomConnectorStart.Connector.CoordinateSystem;
					if (coordinateSystem3.BasisX.IsAlmostEqualTo(coordinateSystem4.BasisX, 0.01) || (coordinateSystem3.BasisX.IsAlmostEqualTo(-coordinateSystem4.BasisX, 0.01) && hycableTray2.IsCableTrayOnLine(hycableTray4.CableTray) && xyz != null))
					{
						result = new HYTeeConnection(document, hycableTray, hycableTray2, hycableTray4, VerticalCableTrayFittingTypeGroup.VerticalTee, xyz).NewVerticalTeeFitting1();
					}
					else if (coordinateSystem3.BasisX.IsAlmostEqualTo(coordinateSystem4.BasisY, 0.01) || coordinateSystem3.BasisX.IsAlmostEqualTo(-coordinateSystem4.BasisY, 0.01))
					{
						if ((int)(document.GetElementById(hycableTray.CableTray.GetTypeId()) as CableTrayType).ShapeType == 2)
						{
							throw new MyException("请检查桥架位置");
						}
						result = new HYTeeConnection(document, hycableTray, hycableTray2, hycableTray4, VerticalCableTrayFittingTypeGroup.VerticalDirectionTee, xyz).NewDirectionVerticalTeeFitting();
					}
				}
				transaction.Commit();
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
                    if ((int)status != 3 && (int)status != 2)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool NewCrossFitting(IList<CableTray> cableTrays)
		{
			bool result = false;
			if (cableTrays.Count < 4)
			{
				throw new System.ArgumentNullException("CableTray", "is null");
			}
			Document document = cableTrays[0].Document;
			if (!Common.IsSameCableTrayType(document, cableTrays))
			{
				throw new MyException("桥架的类型不一致");
			}
			if (Common.IsNoFitting(document, cableTrays[0]))
			{
				throw new MyException("此功能不适用于无配件桥架");
			}
			new List<CableTray>();
			IList<CableTray> list = Common.OrderCableTrayPosition(cableTrays);
			HYCableTray<CableTray> hycableTray = new HYCableTray<CableTray>(list[0]);
			HYCableTray<CableTray> hycableTray2 = new HYCableTray<CableTray>(list[1]);
			HYCableTray<CableTray> hycableTray3 = new HYCableTray<CableTray>(list[2]);
			HYCableTray<CableTray> hycableTray4 = new HYCableTray<CableTray>(list[3]);
			HYCableTray<CableTray> hycableTray5 = null;
			HYCableTray<CableTray> hycableTray6 = null;
			Transaction transaction = null;
			try
			{
				transaction = new Transaction(document, "Cross");
				transaction.Start();
				if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray) && hycableTray3.IsCableTrayOnLine(hycableTray4.CableTray) && hycableTray.IsSamePlan(hycableTray4.CableTray))
				{
					result = Common.NewCrossFittingByRevitCommand(document, ref hycableTray, hycableTray2, ref hycableTray3, hycableTray4, ref hycableTray5, ref hycableTray6);
				}
				else if (hycableTray.IsParallel(hycableTray2.CableTray) && hycableTray.IsZAxis() && hycableTray2.IsZAxis() && hycableTray3.IsSamePlan(hycableTray4.CableTray) && Common.IsVerticalIntersection(hycableTray3, hycableTray4))
				{
					if ((int)(document.GetElementById(hycableTray.CableTray.GetTypeId()) as CableTrayType).ShapeType == 2)
					{
						throw new MyException("请检查桥架位置");
					}
					XYZ lineProject = hycableTray.GetLineProject(hycableTray3.EndXYZ);
					hycableTray5 = Common.SeparateCableTray(ref hycableTray, hycableTray2, lineProject);
					Transform coordinateSystem = hycableTray.CustomConnectorStart.Connector.CoordinateSystem;
					Transform coordinateSystem2 = hycableTray3.CustomConnectorStart.Connector.CoordinateSystem;
					Transform coordinateSystem3 = hycableTray4.CustomConnectorStart.Connector.CoordinateSystem;
					HYCableTray<CableTray> hycableTray7 = null;
					HYCableTray<CableTray> hycableTray8 = null;
					if (coordinateSystem.BasisX.IsAlmostEqualTo(coordinateSystem2.BasisX, 0.01) || coordinateSystem.BasisX.IsAlmostEqualTo(-coordinateSystem2.BasisX, 0.01))
					{
						hycableTray7 = hycableTray3;
						hycableTray8 = hycableTray4;
					}
					else if (coordinateSystem.BasisX.IsAlmostEqualTo(coordinateSystem3.BasisX, 0.01) || coordinateSystem.BasisX.IsAlmostEqualTo(-coordinateSystem3.BasisX, 0.01))
					{
						hycableTray7 = hycableTray4;
						hycableTray8 = hycableTray3;
					}
					if (hycableTray7 != null && hycableTray8 != null)
					{
						lineProject = hycableTray3.GetLineProject(hycableTray4.EndXYZ);
						result = new HYCrossConnection(document, hycableTray, hycableTray5, hycableTray7, hycableTray8, VerticalCableTrayFittingTypeGroup.VerticalLeftRightCross, lineProject).NewDirectionVerticalCrossFitting();
					}
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray) && hycableTray.IsSamePlan(hycableTray2.CableTray) && hycableTray.IsXYPlan() && ((hycableTray3.IsSamePlan(hycableTray.CableTray) && Common.IsVerticalIntersection(hycableTray, hycableTray3) && hycableTray4.IsZAxis()) || (hycableTray4.IsSamePlan(hycableTray.CableTray) && Common.IsVerticalIntersection(hycableTray, hycableTray4) && hycableTray3.IsZAxis())))
				{
					HYCableTray<CableTray> hycableTray9 = null;
					HYCableTray<CableTray> hycableTray10 = null;
					if (hycableTray3.IsZAxis() && hycableTray4.IsXYPlan())
					{
						hycableTray9 = hycableTray3;
						hycableTray10 = hycableTray4;
					}
					else if (hycableTray4.IsZAxis() && hycableTray3.IsXYPlan())
					{
						hycableTray9 = hycableTray4;
						hycableTray10 = hycableTray3;
					}
					if (hycableTray9 == null || hycableTray10 == null)
					{
						throw new MyException("请检查桥架位置");
					}
					if (Common.IsVerticalIntersection(hycableTray, hycableTray10) && hycableTray.IsSamePlan(hycableTray10.CableTray))
					{
						XYZ intersectionPoint = hycableTray.IntersectionPoint(hycableTray10.CableTray);
						hycableTray5 = Common.SeparateCableTray(ref hycableTray, hycableTray2, intersectionPoint);
						result = new HYCrossConnection(document, hycableTray9, hycableTray10, hycableTray, hycableTray5, VerticalCableTrayFittingTypeGroup.VerticalUpDownCross, intersectionPoint).NewUpDownVerticalCrossFitting();
					}
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray) && hycableTray.IsXYPlan() && hycableTray3.IsParallel(hycableTray4.CableTray) && hycableTray3.IsZAxis())
				{
					if ((int)(document.GetElementById(hycableTray.CableTray.GetTypeId()) as CableTrayType).ShapeType == 2)
					{
						throw new MyException("请检查桥架位置");
					}
					XYZ lineProject2 = hycableTray3.GetLineProject(hycableTray.EndXYZ);
					hycableTray6 = Common.SeparateCableTray(ref hycableTray3, hycableTray4, lineProject2);
					lineProject2 = hycableTray.GetLineProject(hycableTray3.EndXYZ);
					hycableTray5 = Common.SeparateCableTray(ref hycableTray, hycableTray2, lineProject2);
					result = new HYCrossConnection(document, hycableTray3, hycableTray6, hycableTray, hycableTray5, VerticalCableTrayFittingTypeGroup.VerticalCross, lineProject2).NewVerticalCrossFitting();
				}
				else if (hycableTray3.IsCableTrayOnLine(hycableTray4.CableTray) && hycableTray3.IsXYPlan() && hycableTray.IsParallel(hycableTray2.CableTray) && hycableTray.IsZAxis())
				{
					if ((int)(document.GetElementById(hycableTray.CableTray.GetTypeId()) as CableTrayType).ShapeType == 2)
					{
						throw new MyException("请检查桥架位置");
					}
					XYZ lineProject3 = hycableTray.GetLineProject(hycableTray3.EndXYZ);
					hycableTray5 = Common.SeparateCableTray(ref hycableTray, hycableTray2, lineProject3);
					lineProject3 = hycableTray3.GetLineProject(hycableTray.EndXYZ);
					hycableTray6 = Common.SeparateCableTray(ref hycableTray3, hycableTray4, lineProject3);
					result = new HYCrossConnection(document, hycableTray, hycableTray5, hycableTray3, hycableTray6, VerticalCableTrayFittingTypeGroup.VerticalCross, lineProject3).NewVerticalCrossFitting();
				}
				transaction.Commit();
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
                    if ((int)status != 3 && (int)status != 2)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
